package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"novel-mgmt/cache"
	"novel-mgmt/models"
	"path/filepath"
	"strconv"
	"time"
)

type BookControllers struct {
	beego.Controller
}

type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *BookControllers) Add() {
	title := c.GetString("title")
	author := c.GetString("author")
	count := c.GetString("count")
	types := c.GetString("types")
	img := c.GetString("img")
	if title == "" || author == "" || count == "" || types == "" || img == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "接收的参数不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	book := models.Book{
		Title:  title,
		Author: author,
		Count:  count,
		Types:  types,
		Img:    img,
	}
	err := models.DB.Create(&book).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "添加成功",
	}
	c.ServeJSON()
	return
}

func (c *BookControllers) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "获取文件失败",
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	ext := filepath.Ext(header.Filename)
	if ext != ".jpg" && ext != ".png" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "小说格式不正确",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if header.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "图片不能超过2MB",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "上传成功",
		Data: "http://127.0.0.1:8080/" + "static/img/" + header.Filename,
	}
	c.ServeJSON()
	return
}

func (c *BookControllers) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	f := (page - 1) * size
	var cou int64
	var book []models.Book
	query := models.DB.Model(&models.Book{})
	query.Count(&cou)
	query.Offset(f).Limit(size).Find(&book)
	fxd := make(map[string]interface{})
	fxd["count"] = cou
	fxd["book"] = book
	data := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, data).Result()
	if cacheJson == "" {
		json, _ := json.Marshal(book)
		cache.Rdb.Set(cache.Ctx, data, json, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheJson), &book)
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: fxd,
		}
		c.ServeJSON()
		return
	}
}

func (c *BookControllers) Del() {
	id, _ := c.GetInt("id")
	var book []models.Book
	models.DB.Delete(&book, id)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()
	return
}

func (c *BookControllers) Detail() {
	id, _ := c.GetInt("id")
	var book models.Book
	data := "data_id" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, data).Result()
	if cacheJson == "" {
		models.DB.Model(&models.Book{}).Where("id=?", id).First(&book)
		json, _ := json.Marshal(book)
		cache.Rdb.Set(cache.Ctx, data, json, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheJson), &book)
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "详情展示成功",
			Data: book,
		}
		c.ServeJSON()
		return
	}

}
